﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;

namespace PaulBeck.Timesheet.WebParts.Admin.EditClient
{
    public partial class EditClientUserControl : UserControl
    {

        public int ClientId
        {
            get
            {
                object o = ViewState["ClientId"];
                return (o == null) ? 0 : (int)o;
            }
            set
            {
                ViewState["ClientId"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ClientId = Convert.ToInt32(Page.Request.QueryString["cid"]);
                if (ClientId == 0)  // Insert/Add new
                {
                    cbActive.Checked = true;
                    btnAdd.Visible = true;
                    btnEdit.Visible = false;
                    btnDelete.Visible = false;
                }
                else
                {
                    btnAdd.Visible = false;
                    btnEdit.Visible = true;
                    btnDelete.Visible = true;
                    // Edit and delete is available
                    TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                    var client = (from c in dc.Clients
                                  where c.ClientId == ClientId
                                  select new
                              {
                                  c.Description,
                                  c.Name,
                                  c.Active,
                              }).Single();
                    if (client != null)
                    {
                        txtName.Text = client.Name;
                        txtDesc.Text = client.Description;
                            if (client.Active == true)
                            {
                                cbActive.Checked = true;
                            }
                    }
                }
            }

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            Client client = new Client();
            client.Name = txtName.Text;
            client.Description = txtDesc.Text;
            client.Active = cbActive.Checked;
            dc.Clients.InsertOnSubmit(client);
            dc.SubmitChanges();
            Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('ClientAdd');</script>");
            Context.Response.Flush();
            Context.Response.End();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
            Client client = dc.Clients.Single(c => c.ClientId == ClientId);
            client.Name = txtName.Text;
            client.Description = txtDesc.Text;
            client.Active = cbActive.Checked;
            dc.SubmitChanges();
            Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('ClientEdit');</script>");
            Context.Response.Flush();
            Context.Response.End();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                TSDBDataContext dc = new TSDBDataContext(BOL.Common.GetTimesheetConnectioninfo());
                Client client = dc.Clients.Single(c => c.ClientId == ClientId);
                dc.Clients.DeleteOnSubmit(client);
                dc.SubmitChanges();
                Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup('ClientDelete');</script>");
                Context.Response.Flush();
                Context.Response.End();
            }
            catch (Exception ex)
            {
                lblError.Text = "Error: The Client cannot be deleted as it has dependancies.  <br/>Please ensure that all projects associated with this ckient are removed before deleting this client.";
            }
        }
    }
}
